﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using fmtx.Utility;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Text;


public static class StaticCs
{
    #region 大图转缩略图
    /// <summary>
    /// 大图转缩略图
    /// </summary>
    /// <param name="file"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="mode"></param>
    /// <returns></returns>
    public static ImageResult MakeThumbnail(string file, int width, int height, ImageFactory.ImgModel mode)
    {
        ImageResult result = new ImageResult();
        result.FileName = "";
        if (string.IsNullOrEmpty(file))
        {
            return result;
        }
        try
        {
            HttpContext.Current.Server.MapPath(StaticCs.VirtualPath + file);
        }
        catch (Exception)
        {
            file = "";
        }
        string filename = Path.GetFileName(file);
        if (string.IsNullOrEmpty(filename)) return result;
        string restleFolder = Path.GetDirectoryName(file);
        string originalImagePath = HttpContext.Current.Server.MapPath(StaticCs.VirtualPath + file);
        string thumbFolder = Path.GetDirectoryName(originalImagePath);
        string ext = Path.GetExtension(originalImagePath);
        string name = filename.Substring(0, filename.LastIndexOf('.'));
        string thumbfilename = name + "_" + width + "x" + height + "_" + mode.ToString() + ext;
        string thumbnailPath = thumbFolder + "/thumb/" + thumbfilename;
        if (!Directory.Exists(thumbFolder + "/thumb/"))
        {
            Directory.CreateDirectory(thumbFolder + "/thumb/");
        }
        if (File.Exists(thumbnailPath))
        {
            using (Image originalImage = Image.FromFile(originalImagePath))
            {
                result.Width = originalImage.Width;
                result.Height = originalImage.Height;
            }
        }
        else
        {
            var res = ImageFactory.MakeThumbnail(originalImagePath, thumbnailPath, width, height, mode);
            result.Height = res.Height;
            result.Width = res.Width;
        }
        result.FileName = restleFolder.Replace(@"\","/") + "/thumb/" + thumbfilename;
        return result;
    } 
    #endregion

    
    #region 虚拟路径
    /// <summary>
    /// 虚拟路径
    /// </summary>
    public static string VirtualPath
    {
        get { return ConfigurationManager.AppSettings["VirtualPath"]; }
    } 
    #endregion


}